package org.jadelyre.core;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPool {



    public static ThreadPoolExecutor createThreadPoolExecutor(ThreadPoolEnum threadPoolEnum){
        if (threadPoolEnum.getPoolSize() <= 0 && threadPoolEnum.getMaxPoolSize() <= 0){
            return null;
        }
        LinkedBlockingQueue<Runnable> linkedBlockingQueue = new LinkedBlockingQueue<>();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(JadelyreConfig.availableProcessors() * threadPoolEnum.getPoolSize(), JadelyreConfig.availableProcessors() * threadPoolEnum.getMaxPoolSize(), 600, TimeUnit.SECONDS, linkedBlockingQueue);
        threadPoolExecutor.prestartCoreThread();
        return threadPoolExecutor;
    }


    public enum ThreadPoolEnum{

        /***************************************
         * server端
         *****************************************/
        JADE_SYSTEM_CONNECT(2,3),
        JADE_DECODE(2,3),
        JADE_ENCODE(2,3),
        JADE_HANDLE(2,3),
        JADE_WRITE(2,3),
        JADE_CLOSE(1,1),
        JADE_SCHEDULE(1,1),
        /***************************************
         * client端
         *****************************************/
        LYRE_SYSTEM_CONNECT(2,3),
        LYRE_DECODE(2,3),
        LYRE_ENCODE(2,3),
        LYRE_HANDLE(2,3),
        LYRE_WRITE(2,3),
        LYRE_CLOSE(1,1),
        LYRE_SCHEDULE(1,1);

        private int poolSize;
        private int maxPoolSize;

        ThreadPoolEnum(int poolSize, int maxPoolSize) {
            this.poolSize = poolSize;
            this.maxPoolSize = maxPoolSize;
        }

        public int getPoolSize() {
            return poolSize;
        }

        public int getMaxPoolSize() {
            return maxPoolSize;
        }
    }
}
